// pages/act/home/home.js
let {
  api_uc_GetInfoByToken,
  api_GetAllBanners,
  api_GetAllAuthorities,
  api_GetActList,
  api_LikeAct, 
  api_QueryTopicRefresh
} = require("../../api/getData.js")
const app = getApp()

Page({
  data: {
    swiperList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    previousMargin: 0,
    nextMargin: 0,
    dataSource: [],
    isloading: false,

    imageList:[],

    // 上拉加载
    curPage: 0,
    maxSize: 0,

    currentTab: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onShow(){
    this.onLoad();
  },

  onLoad: function (options) {
    console.log("home页面被加载了")
    this.setData({
      swiperList: [{
        url: '',
        img: '/images/swipper/1.jpg',
        title: '学院路校区'
      }, {
        url: '',
        img: '/images/swipper/2.jpg',
        title: '图书馆'
      }, {
        url: '',
        img: '/images/swipper/3.jpg',
        title: '沙河校区'
      }]
    })

    /*获取主页活动列表*/
    this.getHomeActList();
  },

  // onPullDownRefresh: function () {
  //   // 页面相关事件处理函数--监听用户下拉动作
  //   this.refresh();
  // },
  // onReachBottom: function () {
  //   // 页面相关事件处理函数--监听下拉触底
  //   this.getHomeActList();
  // },
  /**************以上是生命周期********************以下是自定义函数**************************/

  /*获取主页活动列表*/
  getHomeActList: function () {
    if (this.data.bottom >= this.data.maxSize) {
      console.log("==> 到达底部");
      wx.showToast({
        title: '别再拉了，我是有底线的！',
        icon: 'none',
      })
      return
    } else {
      let params = {
        page: this.data.curPage + 1,
        size: 5
      };
      var imgae_list_tmp = []
      let weishalong = []
      api_GetActList(params, (res) => {
        res.data.forEach(i => {
          var tmp = i;
          var imgUrlList = []
          for(var m=0;m<tmp.piclen;m++){
            imgUrlList.push(app.globalData.static_url + "topic/" + tmp.id +"/"+(m+1)+  ".png")
            imgae_list_tmp.push(app.globalData.static_url + "topic/" + tmp.id + "/" + (m + 1) + ".png")
          }
          tmp.imgUrlList = imgUrlList;
          // tmp.imageUrl = app.globalData.static_url + "topic/" + tmp.id + ".png";
          weishalong.push(i)
        })
        this.setData({
          bottom: weishalong.length,
          dataSource: weishalong,
          curPage: 1,
          maxSize: 191,
          imageList: imgae_list_tmp
        })
      })
    }
  },

  bindMakeComment: function (e) {
    var topic = e.currentTarget.dataset.index;
    console.log('当前点击的活动ID==>', topic.id);
    var dataTmp = JSON.stringify(topic);
    wx.navigateTo({
      url: '../../mine/question/question?topic=' + dataTmp,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
    })
  },

  bindImgYulan: function (event) {
    var src = event.currentTarget.dataset.src;//获取data-src
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: this.data.imageList
    })
  },

  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    this.refresh();
  },

  refresh(){
    if (this.data.bottom >= this.data.maxSize) {
      console.log("==> 到达底部");
      wx.showToast({
        title: '别再拉了，我是有底线的！',
        icon: 'none',
      })
      return
    } else {
      let params = {
        page: this.data.curPage + 1,
        size: 5
      };
      var imgae_list_tmp = []
      // let weishalong = this.data.dataSource
      let weishalong = []
      api_QueryTopicRefresh(params, (res) => {
        res.data.forEach(i => {
          var tmp = i;
          var imgUrlList = []
          for (var m = 0; m < tmp.piclen; m++) {
            imgUrlList.push(app.globalData.static_url + "topic/" + tmp.id + "/" + (m + 1) + ".png")
            imgae_list_tmp.push(app.globalData.static_url + "topic/" + tmp.id + "/" + (m + 1) + ".png")
          }
          tmp.imgUrlList = imgUrlList;
          // tmp.imageUrl = app.globalData.static_url + "topic/" + tmp.id + ".png";
          weishalong.push(i)
        })
        this.setData({
          bottom: weishalong.length,
          dataSource: weishalong,
          curPage: 1,
          maxSize: 191,
          imageList: imgae_list_tmp
        })
      })
    }
  },

  //点击切换
  clickTab: function (e) {

    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

})